package main

import (
	"fmt"
	"log"
	"net/http"
	"strconv"
)

//HTTP echo server
func HTTPServer() {
	port := 8989
	log.Println("starting server, listening on port " + strconv.Itoa(port))
	http.HandleFunc("/", EchoHandler)
	http.ListenAndServe(":"+strconv.Itoa(port), nil)
}

// EchoHandler echos back the request as a response
func EchoHandler(writer http.ResponseWriter, request *http.Request) {

	log.Println("Echoing back request made to " + request.URL.Path + " to client (" + request.RemoteAddr + ")")

	writer.Header().Set("Access-Control-Allow-Origin", "*")

	// allow pre-flight headers
	writer.Header().Set("Access-Control-Allow-Headers", "Content-Range, Content-Disposition, Content-Type, ETag")
	// writer.Write([]byte(request.RemoteAddr))
	fmt.Fprintf(writer, "%s", request.RemoteAddr)
}
